<template>
	<view>
		<!-- 轮播图 -->
		<swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000"
			:circular="true">
			<swiper-item v-for="(item,index) in goodsInfo.pics" :key='index'>
				<image :src="item.pics_big" @click="preview(index)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="goods-info">
			<view class="goods-price">
				￥{{goodsInfo.goods_price}}
			</view>
			<!-- 商品信息主体 -->
			<view class="goods-info-body">
				<!-- 商品名称 -->
				<view class="goods-name">
					{{goodsInfo.goods_name}}
				</view>
				<!-- 收藏 -->
				<view class="collect">
					<uni-icons type="star" size="18"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="freight">
				快递：免运费
			</view>
		</view>
		<!-- 商品详情信息 -->
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		<!-- 商品导航 -->
		<view class="goods-nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick"></uni-goods-nav>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				goodsInfo: {},
				options: [{
					icon: 'shop',
					text: '店铺',
				}, {
					icon: 'cart',
					text: '购物车',
					info: 2
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			}
		},

		onLoad(options) {
			const goods_id = options.goods_id
			this.getGoodsDetail(goods_id)
		},
		computed:{
			...mapGetters('my_cart',['total'])
		},
		methods: {
			...mapMutations('my_cart', ['addCart']),
			onClick(e) {
				console.log(e.content.text);
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			buttonClick(e) {
				const {
					goods_id,
					goods_name,
					goods_price,
					goods_small_logo
				} = this.goodsInfo
				// 判断是否点击了购物车
				if (e.content.text === '加入购物车') {
					const goods = {
						goods_id,
						goods_name,
						goods_price,
						goods_small_logo,
						goods_count: 1,
						goods_state: true //是否选中
					}
					this.addCart(goods)
				}
			},
			// 图片预览
			preview(index) {
				uni.previewImage({
					// 当前图片的索引
					current: index,
					// 所有图片的数组
					urls: this.goodsInfo.pics.map(item => item.pics_big)
				})
			},
			async getGoodsDetail(id) {
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id: id
				})
				if (meta.status !== 200) {
					return uni.$showMsg()
				}
				// 改造数据 解决图片底部白边问题
				message.goods_introduce = message.goods_introduce.replace(/<img/g, '<img style="display:block;"')
				this.goodsInfo = message
			}
		}
	,
	watch:{
		// 通过监听器监听total值得变化
		total:{
			handler(newValue){
			const findRes=this.options.find((e)=>e.text=='购物车')
			findRes.info=newValue
		},
		// immediate 属性用来声明此侦听器，是否在页面初次加载完毕后立即调用 （ 使得info的值初始化时就改变）
		immediate:true
		}
	}
	}
</script>

<style lang="scss">
	.swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.goods-info {
		padding: 10px 0 10px 10px;

		.goods-price {
			color: #c00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
				padding-right: 10px;
			}

			.collect {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-left: 1px solid #efefef;
				color: gror;
			}
		}

		.freight {
			margin: 10px 0;
			font-size: 12px;
			color: groy;
		}
	}

	.goods-nav {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>
